<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class User extends CI_Controller {

    public $specialCharactor = "!@#$%^&*()abcxyz0987654312";

    public function __construct() {
        parent::__construct();
        session_start();
        $this->load->library('template');
        $this->load->helper('url');
        $this->load->helper('user_helper');
        $this->template->set_template('default');

        $this->template->add_css('public/css/bootstrap.css');
        $this->template->add_css('public/css/bootstrap-responsive.css');

//        $this->template->add_js('public/js/jquery-1.9.1.js');
        $this->template->add_js('public/js/jquery-1.11.0.min.js');
//        $this->template->add_js('public/js/jquery.min.js');
        $this->template->add_js('public/js/bootstrap.min.js');


        $data["newUrl"] = base_url() . "index.php/event/";
        $data["loginUrl"] = base_url() . "index.php/user/";
        $data["createEventUrl"] = base_url() . "index.php/event/create";
        
        $data["chatUrl"] = base_url() . "index.php/chat/";
        $data["registerUrl"] = base_url() . "index.php/user/register";
        $this->template->write_view('header', 'top_menu', $data, TRUE);
    }

    public function index() {
        if (is_login() == True){
            redirect( base_url().'index.php/user/profile', 'refresh');
            return;
        }
        //Load content, not load file
        $this->template->write('title', 'Đăng nhập');
        //Load file template
        if (isset($_REQUEST['next']) && $_REQUEST['next'] != "") {
            $next = $_REQUEST['next'];
        } else {
            $next = "";
        }

        $data["next"] = $next;
        $data["registerUrl"] = base_url() . "/index.php/user/register";
        $this->template->write_view('content', 'user/login', $data);
        $this->template->write('footer', '');
        // Load and display the template
        $this->template->load();
    }

    public function register() {
        if (is_login() == True){
            redirect( base_url().'index.php/user/profile', 'refresh');
            return;
        }
        
        $this->template->write('title', 'Đăng ký tài khoản');
        $this->template->write_view('content', 'user/register');
        $this->template->load();
    }

    public function profile() {
        $this->template->write('title', 'Trang cá nhân');
        $this->template->write_view('content', 'user/profile');
        $this->template->load();
    }

    public function logout() {
        $_COOKIE["togetherOAuth"] = "";
        session_destroy();
    }

    /**
     * Xử lý login tại đây
     * 
     */
    public function login() {
        //bắt username, pass,remember
        //check sql injection.
        //pass md5 và check trong model user
        //Nếu OK, đá sang user/profile
        //tạo cookie và session
        //Nếu Không ok, quăng lỗi trả về ra view
        $user = $_REQUEST['user'];
        $pass = ($_REQUEST['pass']);
        $check = $_REQUEST['check'];
        $next = $_REQUEST['next'];
       
        if (get_magic_quotes_gpc() == false) {
            $user = mysql_real_escape_string($user);
            $pass = mysql_real_escape_string($pass);
        }
        if ($user == "" and $pass == "") {
            echo '1';
        } elseif ($user == "") {
            echo '2';
        } elseif ($pass == "") {
            echo '3';
        } else { /**
         * @param đưa mật khẩu về dạng md5
         * 
         */
            $pass = md5($pass);
            $this->load->model('muser');
            $login = $this->muser->login($user, $pass);

            $togetherOAuth = md5($user + $pass + $this->specialCharactor);
            if ($login == true) {
                $_SESSION['username'] = $user;
                $_SESSION['password'] = $pass;
                if ($check == 'ok') {
                    setcookie("togetherOAuth", $togetherOAuth, time() + 7 * 24 * 60 * 60,"/");
                } else {
                    setcookie("togetherOAuth", $togetherOAuth, time() - 1,"/");
                }

                echo $next != "" ? $next : "ok";
            } else {
                echo '4';
            }
        }
    }

    public function check_mail() {
        $email = $_REQUEST['email'];
        $this->load->model('muser');

        $check_email = $this->muser->check_email($email);
        if ($check_email == true) {
            echo '1';
        }
    }

    public function check_username() {
        $user = $_REQUEST['user'];
        $this->load->model('muser');
        $check_user = $this->muser->check_use($user);
        if ($check_user == true) {
            echo "1";
        }
    }

    public function create_account() {
        /**
         * lấy dữ liệu từ form 
         */
        $user = $_REQUEST['user'];
        $email = $_REQUEST['email'];
        $pass = md5($_REQUEST['pass']);


        date_default_timezone_set('Asia/Ho_Chi_Minh');
        $time_now = date('Y-m-d H:i:s');
        $data = array(
            'username ' => $user,
            'password ' => $pass,
            'email' => $email,
            'register_time' => $time_now,
        );


        $this->load->model('muser');
        $check_user = $this->muser->check_use($user);

        $check_email = $this->muser->check_email($email);
        if ($check_email == true) {
            echo '5';
            return;
        }

        if ($check_user == true) {
            echo "1";
            return;
        }
        if ($_REQUEST['pass'] != $_REQUEST['pre_pass']) {
            echo "2";
            return;
        }        
        //ok, data is ready, insert it to database
        $register = $this->muser->register($data);
        if ($register == true) {
            $_SESSION['username'] = $user;
            $_SESSION['password'] = $pass;
            $togetherOAuth = md5($user + $pass + $this->specialCharactor);
            setcookie("togetherOAuth", $togetherOAuth, time() + 7 * 24 * 60 * 60, "/");
            echo "ok";
        } else {
            echo '4';
        }
    }

}

